package com.example.malltest.cart.controller;


import com.example.malltest.cart.entity.Cart;
import com.example.malltest.cart.entity.CartDTO;
import com.example.malltest.cart.service.CartService;
import com.example.malltest.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 加人购物车
     * @param cartDTO
     * @return
     */
    @PostMapping("add")
    public R<Void> add(@RequestBody @Validated CartDTO cartDTO){
        cartService.add(cartDTO);
        return R.ok();
    }

    /**
     * 购物车列表
     * @return
     */
    @GetMapping("list")
    public R<List<Cart>> getList(){
        List<Cart> carts = cartService.getList();
        return R.ok(carts);
    }

    /**
     * 删除购物车
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R<Void> delete(@PathVariable Integer id){
        cartService.delete(id);
        return R.ok();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("delete1")
    public R<Void> deleteBatch(@RequestBody List<Integer> ids) {
        cartService.deleteBatch(ids);
        return R.ok();
    }


}